package com.lj.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lj.eduservice.entity.Subject;
import com.lj.eduservice.entity.node.SubjectNode;
import com.lj.eduservice.service.SubjectService;
import com.lj.eduservice.service.impl.SubjectServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程科目 前端控制器
 * </p>
 *
 * @author luojing
 * @since 2021-04-28
 */
@RestController
@RequestMapping("/eduservice/subject")
public class SubjectController {

    @Autowired
    private SubjectService subjectService;

    @PostMapping("/addSubject")
    @ApiOperation("excel导入课程分类")
    public void addSubject(MultipartFile file){
        subjectService.saveSubject(file);
    }

    @GetMapping("/list")
    @ApiOperation("课程分类列表")
    public List<SubjectNode> list(){
       return subjectService.getListTree();
    }

    @ApiOperation("查询出所有科目标题和id的映射")
    @GetMapping("/getAllTitleAndIdMapper")
    public Map<Object, Object> getAllNameAndIdMapper(){
        List<Map<String, Object>> maps = subjectService.listMaps(new QueryWrapper<Subject>().select("id", "title"));
        Map<Object, Object> mapper= new HashMap<>();
        for (Map<String, Object> map : maps) {
            mapper.put(map.get("id"),map.get("title"));
        }
        return mapper;
    }

    @ApiOperation("通过id查询科目")
    @GetMapping("/getSubject/{id}")
    public Subject getSubject(@PathVariable String id) {
        return subjectService.getById(id);
    }

}

